package cn.cqvie.controller;

import cn.cqvie.common.R;
import cn.cqvie.pojo.Category;
import cn.cqvie.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/getList")
    public R<List<Category>> getList(){
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Category::getId);
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);
    }
    @GetMapping("/{page}/{pageSize}/{categoryName}")
    public R<Page> getPage(@PathVariable(name = "page") int page,@PathVariable(name = "pageSize") int pageSize,@PathVariable(name = "categoryName")String categoryName){
        Page<Category> mypage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(categoryName),Category::getName,categoryName);
        categoryService.page(mypage,queryWrapper);
        return R.success(mypage);
    }
}
